package com.ruiysoft.service.sys.clent;

import com.alibaba.fastjson.JSON;
import com.ruiysoft.entity.sys.*;
import com.ruiysoft.security.entity.EntryUser;
import com.ruiysoft.security.entity.IUserDetails;
import com.ruiysoft.service.sys.ISysMicroShopService;
import com.ruiysoft.service.sys.ISysShopPayPlatformService;
import com.ruiysoft.service.sys.ISysShopPersonnelRelationService;
import com.ruiysoft.service.sys.redis.IRedisSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.annotation.Resource;

/**
 * Created by 化增光 on 2018/11/16.
 */
public class LoginContextUtil{

    private static final Logger log = LoggerFactory.getLogger(LoginContextUtil.class);

    //    private ThreadLocal<UserInfo> threadLocal = new ThreadLocal<>();
//
//    private ThreadLocal<SysShopPersonnel> personnelThreadLocal = new ThreadLocal<>();
//
//    private ThreadLocal<SysMicroShop> shopThreadLocal = new ThreadLocal<>();
    @Resource
    IRedisSysUserService iRedisSysUserService;



//    @Resource
//    ISysUserService iSysUserService;

    @Resource
    ISysShopPersonnelRelationService iSysShopPersonnelRelationService;

    @Resource
    ISysMicroShopService iSysMicroShopService;

    @Resource
    ISysShopPayPlatformService iSysShopPayPlatformService;

    private static LoginContextUtil loginContextUtil = new LoginContextUtil();
    private LoginContextUtil(){}

    public static LoginContextUtil instance(){
        return loginContextUtil;
    }

//    /**
//     * 小程序登录:获取当前登录人员信息
//     * @return
//     */
//    public SysShopPersonnel getSysShopPersonnel(){
//        IUserDetails userDetails = (IUserDetails) SecurityContextHolder.getContext()
//                .getAuthentication()
//                .getPrincipal();
//        SysShopPersonnel personnel = iSysShopPersonnelService.selectById(userDetails.getUsername());
//        return personnel;
//    }
//
//    /**
//     * PC端登录:获取当前登录用户信息
//     * @return
//     */
//    public SysUser getSysUser(){
//        IUserDetails userDetails = (IUserDetails) SecurityContextHolder.getContext()
//                .getAuthentication()
//                .getPrincipal();
//
//        return iSysUserService.selectOne(new EntityWrapper<SysUser>().eq("user_name",userDetails.getUsername()));
//    }
//
//    /**
//     * 根据人员信息获取用户(店主)信息
//     * @param personnelId
//     * @return
//     */
//    public SysUser getSysUserByPersonnelId(Long personnelId){
//        return iSysUserService.selectOne(new EntityWrapper<SysUser>().eq("personnel_id",personnelId));
//    }
    public EntryUser getEntryUser(){
        IUserDetails userDetails = (IUserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        EntryUser entryUser = new EntryUser();
        if (null != userDetails.getUserId()){
            entryUser.setSysUser(iRedisSysUserService.selectSysUserById(userDetails.getUserId()));
        }

        if (null != userDetails.getPersonnelId()){
            SysShopPersonnel personnel = iRedisSysUserService.selectSysShopPersonnelById(userDetails.getPersonnelId());
            Long shopId = iRedisSysUserService.getCurrentLoginOnShopId(userDetails.getLoginType(),null == entryUser.getSysUser()?null:entryUser.getSysUser().getId(),userDetails.getPersonnelId());
            log.info("shopid---->:{}",shopId);
            log.info("personnelid---->:{}",userDetails.getPersonnelId());
            if (null != shopId && null != userDetails.getPersonnelId()){
                SysShopPersonnelRelation personnelRelation = iSysShopPersonnelRelationService.selectByShopIdAndPersonnelId(shopId,personnel.getId());
                log.info("SysShopPersonnelRelation---->:{}", JSON.toJSONString(personnelRelation));
                if(personnelRelation != null) {
                    personnel.setMemberFlag(personnelRelation.getMemberFlag());
                    personnel.setMemberLevel(personnelRelation.getMemberLevel());
                }
                log.info("SysShopPersonnel---->:{}",JSON.toJSONString(personnel));
//                entryUser.setPersonnelRelation(personnelRelation);
            }
            entryUser.setPersonnel(personnel);
        }
        entryUser.setLoginType(userDetails.getLoginType());
        return entryUser;
    }

    public SysUser getSysUser(){
        IUserDetails userDetails = (IUserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();

        if (null != userDetails.getUserId()){
          return iRedisSysUserService.selectSysUserById(userDetails.getUserId());
        }
        return null;
    }
    /**
     * 小程序登录: 根据人员信息获取登录的店铺
     * @return
     */
    public SysMicroShop getEntryMicroShop(){
        EntryUser user = getEntryUser();
        Long shopId = iRedisSysUserService.getCurrentLoginOnShopId(user.getLoginType(),null == user.getSysUser()?null:user.getSysUser().getId(),null == user.getPersonnel()?null:user.getPersonnel().getId());
        if (null == shopId){
            return null;
        }
        return iSysMicroShopService.getById(shopId);
    }

//    /**
//     * 小程序登录: 根据人员信息获取登录的店铺
//     * @return
//     */
//    public SysMicroShop getMicroShopLoginByPersonnelId(Long personnelId){
//        Long shopId = iRedisSysUserService.getCurrentLoginOnShopId(personnelId);
//        if (null == shopId){
//            return null;
//        }
//        return iSysMicroShopService.selectById(shopId);
//    }

//    /**
//     * PC登录: 根据用户信息获取登录的店铺
//     * @return
//     */
//    public SysMicroShop getMicroShopLoginByUserId(){
//        SysUser user = getSysUser();
//        Long shopId = iRedisSysUserService.getUserLoginShop(user.getId());
//        if (null == shopId){
//            return null;
//        }
//        return iSysMicroShopService.selectById(shopId);
//    }

//    private UserInfo getUserInfo(){
//        SysShopPersonnel personnel = this.getShopPersonnel();
//        Long shopId = iRedisSysUserService.getCurrentLoginOnShopId(personnel.getId());
//       /* SysShopPersonnelRelation relation = iSysShopPersonnelRelationService.selectOne(new EntityWrapper<SysShopPersonnelRelation>().eq("personnel_id",personnel.getId()).eq("shop_id",shopId));*/
//        UserInfo userInfo = new UserInfo()
//                .setPersonnelId(personnel.getId())
//                .setOpenid(personnel.getOpenid())
//                .setShopId(shopId)
//                .setHeadImgUrl(personnel.getHeadImgUrl())
//                .setNickname(personnel.getNickname())
//                .setMobile(personnel.getMobile())
//               /* .setIdentityType(relation.getIdentityType())*/;
//        return userInfo;
//    }

//    private SysShopPersonnel getShopPersonnel() {
//        IUserDetails userDetails = (IUserDetails) SecurityContextHolder.getContext()
//                .getAuthentication()
//                .getPrincipal();
//        SysShopPersonnel personnel = iSysShopPersonnelService.selectById(userDetails.getUsername());
//        return personnel;
//    }

//    public SysShopPayPlatform getShopPayPlatform() {
//        SysMicroShop microShop = this.getMicroShop();
//        SysShopPayPlatform payPlatform = iSysShopPayPlatformService.selectOne(new EntityWrapper<SysShopPayPlatform>().eq("shop_id",microShop.getId()));
//        if (null == payPlatform){
//            payPlatform = iSysShopPayPlatformService.selectById(1).setShopId(microShop.getId());
//            payPlatform.setShopId(microShop.getId());
//        }
//        return payPlatform;
//    }

    public SysShopPayPlatform getShopPayPlatformByAppId(String appId) {
        SysShopPayPlatform payPlatform = iRedisSysUserService.selectSysShopPayPlatformByAppId(appId);
        return payPlatform;
    }

//    public SysShopPayPlatform getShopPayPlatformByShopId(Long shopId) {
//        Long id = (null == shopId) ? 0 : shopId;
//        return iSysShopPayPlatformService.selectOne(new EntityWrapper<SysShopPayPlatform>().eq("shop_id",id));
//    }
}
